package com.digdes.calendar.gwt.client.service;

import com.digdes.calendar.gwt.client.widget.*;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.datepicker.client.DatePicker;

import java.util.Date;

public class Calendar implements EntryPoint, ValueChangeHandler<Date> {
    private DockPanel mainPanel = new DockPanel();
    private MonthWidget monthW = new MonthWidget();
    private WeekWidget weekW = new WeekWidget();
    private DayWidget dayW = new DayWidget();
    private DatePicker dateBox = new DatePicker();
    private TabPanel tabPanel = new TabPanel();
    private VerticalPanel leftPanel = new VerticalPanel();
    private CreateEventDialog createEventDialog;
    private Plan plan = new Plan();
    private FlowPanel fp = new FlowPanel();

    public Calendar() {

    }

    public void onModuleLoad() {
        createEventDialog = new CreateEventDialog(new CreateFileDialog(), new CreateParticipantDialog(), plan, new CreatePlace());
        final Button createEventBtn = new Button("Создать событие");
        createEventBtn.setStyleName("createBtn");
        dateBox.addValueChangeHandler(this);
        //dateBox.setSize("200px","200px");
        // panel.setAnimationDuration(500);
        tabPanel.setSize("1010px", "550px");
        tabPanel.add(monthW, "Месяц");
        tabPanel.add(weekW, "Неделя");
        tabPanel.add(dayW, "День");
        tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
            public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
                if (tabPanel.getTabBar().getSelectedTab() == 0) {
                     createEventBtn.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            createEventDialog.center();
                        }
                    });
                    createEventDialog.save.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            createEventDialog.saveEvent();
                            monthW.drawCalendar();
                        }
                    });
                    dayW.getEventsPanel().removeFromParent();
                    leftPanel.add(monthW.getEventsPanel());
                    monthW.drawCalendar();
                }
                if (tabPanel.getTabBar().getSelectedTab() == 1) {
                    weekW.setDataWeekHeader();
                    weekW.getEvents();
                    dayW.getEventsPanel().removeFromParent();
                    monthW.getEventsPanel().removeFromParent();

                }
                if (tabPanel.getTabBar().getSelectedTab() == 2) {
                    dayW.getEvents();
                    monthW.getEventsPanel().removeFromParent();
                    leftPanel.add(dayW.getEventsPanel());
                }
            }
        });

        tabPanel.selectTab(0);

        leftPanel.add(dateBox);
        leftPanel.add(createEventBtn);
        leftPanel.add(monthW.getEventsPanel());

        mainPanel.add(leftPanel, DockPanel.WEST);
        mainPanel.add(tabPanel, DockPanel.EAST);
        RootPanel.get("main").add(mainPanel);

        plan.setCreateEventDialog(createEventDialog);
        fp.add(plan);
        RootPanel.get("scheduler").add(fp);
        RootPanel.get("scheduler").setVisible(false);
    }

    public void onValueChange(ValueChangeEvent<Date> event) {
        boolean bM = false;

        if (monthW.date.getMonth() != event.getValue().getMonth())
            bM = true;

        if (tabPanel.getTabBar().getSelectedTab() == 0) {
            monthW.date.setDatePickierDate(event.getValue());
            if (bM) {
                monthW.drawCalendar();
            }
        }

        if (tabPanel.getTabBar().getSelectedTab() == 1) {
            weekW.date.setDatePickierDate(event.getValue());
            weekW.setDataWeekHeader();
            weekW.getEvents();
        }

        if (tabPanel.getTabBar().getSelectedTab() == 2) {
            dayW.date.setDatePickierDate(event.getValue());
            dayW.getEvents();
            leftPanel.add(dayW.getEventsPanel());
        }

    }
}